<!--
  * 产品
  *
  * @Author:    lee
  * @Date:      2022-12-13 18:28:03
  * @Copyright  lee
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="任务类型" class="smart-query-form-item">
        <a-select v-model:value="queryForm.status" style="width: 130px" :showSearch="true" :allowClear="true" placeholder="任务类型">
          <a-select-option v-for="item in statusList" :key="item" :value="item">
            {{ item }}
          </a-select-option>
        </a-select>
      </a-form-item>



      <a-form-item label="优先级" class="smart-query-form-item">
        <a-select v-model:value="queryForm.first" style="width: 140px" :showSearch="true" :allowClear="true" placeholder="优先级">
          <a-select-option v-for="item in firstList" :key="item" :value="item">
            {{ item }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="申请人" class="smart-query-form-item">
        <a-input style="width: 150px" v-model:value="queryForm.code" placeholder="申请人" />
      </a-form-item>
      <a-form-item label="申请人部门" class="smart-query-form-item">
        <a-select v-model:value="queryForm.department" style="width: 140px" :showSearch="true" :allowClear="true" placeholder="申请人部门">

        </a-select>
      </a-form-item>


      <a-form-item label="工单编号" class="smart-query-form-item">
        <a-input style="width: 150px" v-model:value="queryForm.code" placeholder="工单编号" />
      </a-form-item>


      <a-form-item label="创建时间" class="smart-query-form-item">
        <a-range-picker v-model:value="queryForm.createTime" :ranges="defaultTimeRanges" style="width: 300px" @change="onChangeCreateTime" />
      </a-form-item>
      <a-form-item label="完成时间" class="smart-query-form-item">
        <a-range-picker v-model:value="queryForm.updateTime" :ranges="defaultTimeRanges" style="width: 300px" @change="onChangeCreateTime" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="queryData">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
        <a-button @click="report" class="smart-margin-left10">
          <template #icon>
            <DownloadOutlined />
          </template>
          导出
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns"  :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table
        size="small"
        :dataSource="tableData"
        :columns="columns"
        rowKey="id"
        bordered
        :loading="tableLoading"
        :pagination="false"
        @change="handleTableChange"
    >

      <template #bodyCell="{ text, record, column }">

        <template v-if="column.dataIndex === 'deadLine'">
        <span v-if="text === '超时'">
          <a-tag color="red">
            {{text}}
          </a-tag>
        </span>
          <span v-if="text === '即将超时'">
             <a-tag color="yellow">
            {{text}}
          </a-tag>
        </span>
        </template>

        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="toDetail(record)" type="link">领取</a-button>
            <a-button @click="toDetail(record)" type="link">编辑</a-button>
            <a-button @click="toDetail(record)" type="link">转派</a-button>
            <a-button @click="toDetail(record)" type="link">撤销</a-button>
            <a-button @click="toDetail(record)" type="link">查看</a-button>
          </div>
        </template>
      </template>
    </a-table>



    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
import { reactive, ref, onMounted, watch } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { smartSentry } from '/@/lib/smart-sentry';
import TableOperator from '/@/components/support/table-operator/index.vue';
import { defaultTimeRanges } from '/@/lib/default-time-ranges';
import { useRoute, useRouter } from 'vue-router';
import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';

// ---------------------------- 表格列 ----------------------------
const route = useRoute();
const props = defineProps({
  activeKey: {
    type: String,
    default: '2',
  },
});
//待分配、处理中、已完成、已驳回、已终止
const statusList = ref(['报销单审核','付款单审核','费用申请审核','全部']);
const typeList = ref(['报销单工单','付款单工单','费用申请工单','异常申诉工单']);
const firstList = ref([]);

const columns = ref([
  {
    title: '任务ID',
    dataIndex: 'code',
    ellipsis: true,
    sorter: true,
    width: 160,
  },
  {
    title: '任务名称',
    dataIndex: 'name',
    ellipsis: true,
    sorter: true,
    width: 160,
  },


  {
    title: '申请人',
    dataIndex: 'createBy',
    ellipsis: true,
    sorter: true,
  },
  {
    title: '申请人部门',
    dataIndex: 'departmentName',
    ellipsis: true,
    sorter: true,
  },

  {
    title: '提交时间',
    dataIndex: 'createTime',
    ellipsis: true,
    sorter: true,
  },


  {
    title: '处理期限',
    dataIndex: 'deadLine',
    ellipsis: true,
    sorter: true,
  },
  {
    title: '关联工单编号',
    dataIndex: 'ticket',
    ellipsis: true,
    sorter: true,
    width: 180,
  },

  {
    title: '当前状态',
    dataIndex: 'status',
    ellipsis: true,
    sorter: true,
    width: 90,
  },
  {
    title: '当前处理人',
    dataIndex: 'cby',
    ellipsis: true,
    sorter: true,
    width: 110,
  },
  {
    title: '优先级',
    dataIndex: 'first',
    ellipsis: true,
    width: 70,
  },


  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: 220,
  },
]);

// ---------------------------- 查询数据表单和方法 ----------------------------

const queryFormState = {
  status: '全部', //背调订单编号
  name: undefined, //候选人姓名
  idCard: undefined, //候选人证件号码
  orgId: undefined, //拟应聘公司
  departmentId: undefined, //组织架构
  code: undefined, //背调工单编号
  handlerName: route.query.handlerName, //处理人
  contacts: undefined, //联系人
  contactsPhone: undefined, //联系人电话号码
  createTime: [], //创建时间
  createTimeBegin: undefined, //创建时间 开始
  createTimeEnd: undefined, //创建时间 结束
  pageNum: 1,
  pageSize: 10,
};
// 查询表单form
const queryForm = reactive({ ...queryFormState });
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);

// 重置查询条件
function resetQuery() {
  let pageSize = queryForm.pageSize;
  Object.assign(queryForm, queryFormState);
  queryForm.pageSize = pageSize;
  departmentValue.value = undefined;
  queryData();
}

// 查询数据
async function queryData() {
  tableLoading.value = true;
  try {
    if (route.query.queryForm != '[object Object]') {
      if (typeof route.query.queryForm != 'undefined') {
        if (route.query.queryForm != JSON.stringify(queryForm)) {
          Object.assign(queryForm, JSON.parse(route.query.queryForm));
          delete route.query.queryForm;
        }
      }
    }
    let queryResult ={
      "code": 0,
      "level": null,
      "msg": "success",
      "ok": true,
      "data": {
        "pageNum": 1,
        "pageSize": 10,
        "total": 3974,
        "pages": 398,
        "list": [
          {
            "id": 32322,
            "code": "20230530NO00019",
            "name": "报销单审核 - 张三 - 202405 差旅费",
            "createBy": "李琦珊",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "超时",
            "ticket": "TD20230530NO00019",
            "status": "待处理",
            "cby": "杨颖",
            "first": "高"


          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 李四",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 赵五",
            "createBy": "王云",
            "departmentName": "Q大连XX发展公司",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 张飞",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 赵韵",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 刘备",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 吕布",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 孙权",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 马术",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
          {
            "id": 32465,
            "code": "20230530NO00051",
            "name": "报销单审核 - 刘璇",
            "createBy": "王云",
            "departmentName": "Q武汉爱家国际华城",
            "createTime": "2023-05-30 09:24:47",
            "deadLine": "即将超时",
            "ticket": "TD20230530NO00051",
            "status": "待处理",
            "cby": "黄晓霞",
            "first": "中"
          },
        ],
        "emptyFlag": false
      }
    }

    tableData.value = queryResult.data.list;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}
function handleTableChange(pagination, filters, sorter, currentDataSource) {
  const sortItem = setSorter(sorter);
  queryForm.sortItemList = [];
  queryForm.sortItemList.push(sortItem);
  queryData();
}

function setSorter(sorter) {
  let re = 'ascend' === sorter.order;

  const sortItem = { column: toLine(sorter.field), isAsc: re };

  return sortItem;
}

function toLine(name) {
  return name.replace(/([A-Z])/g, '_$1').toLowerCase();
}
function onChangeCreateTime(dates, dateStrings) {
  queryForm.createTimeBegin = dateStrings[0];
  queryForm.createTimeEnd = dateStrings[1];
}

onMounted(() => {
  queryData();
});

// ---------------------------- 添加/修改 ----------------------------
const formRef = ref();

function showForm(data) {
  formRef.value.show(data);
}

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys;
}

// 进入详情
const router = useRouter();
function toDetail(data) {
  router.push({
    path: '/task/task-detail',
    query: {
      ticketTaskId: data.id,
      id: data.biId,
      backPath: '/task/list',
      queryForm: JSON.stringify(queryForm),
      TabactiveKey: props.activeKey,
    },
  });
}

const treeData = ref();
const departmentValue = ref();
const departmentSearchValue = ref('');


// 导出数据
async function report() {
  try {
  //  await ticketApi.reportExceptionData(queryForm);
  } catch (e) {
    smartSentry.captureError(e);
  }
}
</script>
